package com.geepi.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.geepi.biz.UserBiz;
import com.geepi.biz.UserBizImpl;
import com.geepi.entity.Page;
import com.geepi.entity.User;
import com.geepi.util.DbUtil;


@SuppressWarnings("serial")
public class UserServlet extends HttpServlet {
	
   private UserBiz ub  = new UserBizImpl();//建立业务逻辑层接口实现对象实例
	
	public UserServlet() {
		super();
	}

	
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
    
		doPost(request,response);
		System.out.println("doGet is running --------");
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//System.out.println("user   Servlet is　　running");
		
		
		
		String flag  =  request.getParameter("flag");
		if("userList".equals(flag)){
			
			getUserList(request,response);
			
		}else if("editUser".equals(flag)){
			 
			editUser(request,response);
			
		}else if("delUser".equals(flag)){
			
			delUser(request,response);
		}else if("addUser".equals(flag)){
			
			addUser(request,response);
		}else if("getOneUser".equals(flag)){
			
			getOneUser(request,response);
		}
		
		
	}

    private void addUser(HttpServletRequest request,
			HttpServletResponse response) {
    	       //1、接受用户输入的参数,新增用户，ID是自动填充，不需输入
    			String userName = request.getParameter("userName");
    			String age = request.getParameter("age");
    			String password = request.getParameter("password");
    			String gender = request.getParameter("gender");
    			String height = request.getParameter("height");
    			String birthday = request.getParameter("birthday");
    			//2、封装成MODEl
    			User user = new  User();
    			user.setUserName(userName);
    			user.setAge(Integer.parseInt(age));
    			user.setGender(Integer.parseInt(gender));
    			user.setPassword(password);
    			user.setHeight(Double.parseDouble(height));
    			user.setBirthday(birthday);
    			
    			//3、调用业务逻辑层进行处理
    			int result = ub.addUser(user);
    			
    			//4、根据返回的结果，跳转页面
    			System.out.println("result>>>>"+result);//测试
    			
    			if(result==1){
    			try {
    				response.sendRedirect("user?flag=userList");
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}

    }
    


	//删除用户
	private void delUser(HttpServletRequest request,
			HttpServletResponse response) {

		String userId = request.getParameter("userId");
		int result =  ub.delUser(userId);
		System.out.println("del----result-->>>>"+result);
		if(result==1){
			try {
				response.sendRedirect("user?flag=userList");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


	private void getOneUser(HttpServletRequest request,
			HttpServletResponse response) {
		
		
		
	}


	private void editUser(HttpServletRequest request, HttpServletResponse response) {
	
		
		//接受用户输入的参数
		String userId = request.getParameter("userId");
		String userName = request.getParameter("userName");
		System.out.println("edit username:" + userName);
		String age = request.getParameter("age");
		String password = request.getParameter("password");
		String  gender = request.getParameter("gender");
		//封装成MODEl
		User user = new  User();
		user.setUserId(Integer.parseInt(userId));
		user.setUserName(userName);
		user.setAge(Integer.parseInt(age));
		user.setGender(Integer.parseInt(gender));
		user.setPassword(password);
		//调用业务逻辑层进行处理
		int result = ub.updateUser(user);
		
		//根据返回的结果，跳转页面
		System.out.println("result>>editUser   >>"+result);//测试
		
		if(result==1){
		try {
			response.sendRedirect("user?flag=userList");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	}
	

	private void getUserList(HttpServletRequest request, HttpServletResponse response) {
		//用于分页
		Page  page = new Page();
		String currPage = request.getParameter("currPage");//获取点击页面转换时，传的currPage的值
		if(currPage==null){
			currPage =  "1";
		}
		int pageIndex = Integer.parseInt(currPage);//类型转换
		page.setCurrPage(pageIndex);
		int temp = DbUtil.getTotal("myuser")/Page.PAGE_SIZE;
		int totalPage =  DbUtil.getTotal("myuser")%Page.PAGE_SIZE==0?temp:temp+1;
		
		/*
		 *此方法有弊端，会四舍五入得到的页面数，会减少结果显示数量
		 * int totalPage =  (int)Math.ceil((double)(DbUtil.getTotal("myuser")/Page.PAGE_SIZE));
		
		*
		*/
		
		System.out.println("totalPage-------"+totalPage);
		page.setTotalPage(totalPage);
		if(pageIndex<1){
		    pageIndex= 1;
		} else if(pageIndex >totalPage ){
			pageIndex = totalPage;
			
		}
		
		
		//用于查询单个用户
		String userId = request.getParameter("userId");
		
		
		List<User>  userList = ub.getUserList(userId,page);//调用业务逻辑层实现对象的方法
		
		System.out.println("userId="+userId);
		
		request.setAttribute("userList", userList);
		try {
			if(userId==null){
			request.setAttribute("page", page);
			request.getRequestDispatcher("/3/userList.jsp").forward(request,response);
			}else{
			request.getRequestDispatcher("/3/doEditUser.jsp").forward(request,response);
			}
		
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
	}


	public void init() throws ServletException {
		// Put your code here
	}
	
	
	
	

}
